﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ServerManager.Classes;
using System.Reflection;

namespace ServerManager
{
    public partial class frmManageObject : Form
    {
        public frmManageObject(Object obj)
        {
            InitializeComponent();

            Type t = obj.GetType();
            this.Text = "Manage " + t.Name;
            this.lblNewObject.Text = "Manage " + t.Name;

            PropertyInfo[] propInfo = t.GetProperties();
            this.Size = new Size(310, 100 + propInfo.Length * 28);


            for (int i = 0; i < propInfo.Length; i++)
            {
                #region Add Label
                Label lbl = new Label();
                lbl.AutoSize = true;
                lbl.Name = "lbl" + propInfo[i].Name;
                lbl.Location = new Point(10, 28 * i + 40);
                lbl.Text = propInfo[i].Name + ":";
                this.Controls.Add(lbl);
                #endregion

                if (propInfo[i].PropertyType == typeof(bool))
                {
                    #region Add Checkbox
                    CheckBox cb = new CheckBox();
                    cb.Name = "cb" + propInfo[i].Name;
                    cb.Checked = (Boolean)propInfo[i].GetValue(obj, null);
                    cb.Location = new Point(265, 28 * i + 40);
                    this.Controls.Add(cb);
                    #endregion
                }
                else
                {
                    #region Add Textbox
                    TextBox txt = new TextBox();
                    txt.Name = "txt" + propInfo[i].Name;

                    Object propObj = propInfo[i].GetValue(obj, null);
                    if (propObj != null)
                    {
                        txt.Text = propObj.ToString();
                    }

                    txt.Location = new Point(180, 28 * i + 40);
                    this.Controls.Add(txt);
                    #endregion
                }
            }

            #region Add Savebutton
            Button btnSave = new Button();
            btnSave.Name = "btnSave";
            btnSave.Text = "Save";
            btnSave.Click += new EventHandler(btnSave_Click);
            btnSave.Location = new Point(180, 40 + propInfo.Length * 28);
            this.Controls.Add(btnSave);
            #endregion
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }
    }
}
